package org.jeecg.modules.demo.zjzx.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.util.Calendar;
import java.util.Date;

public class CalculateUtils {
    public static void main(String[] args) {
        System.err.println(getYyyyMmDd(2020,3));
        //打印 2020-01-03
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        System.out.println(getDaysInYear(date));

        //打印 3
    }
    /**
     *  知道年和天算 年-月-日
     * @param year 年
     * @param day  第几天
     * @return
     */
    public static Date getYyyyMmDd(int year,int day) {
        DateTime parse = DateUtil.parse(String.format("%s-01-00", year), "yyyy-MM-dd");
        DateTime dateTime = DateUtil.offsetDay(parse, day);
        return dateTime;
    }
    /**
     * get days in this year
     * 年月日算第几天
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static int getDaysInYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH)+1;
        int day=calendar.get(Calendar.DATE);
        /*平年二月28天*/
        final int DAYS_28 = 28;
        /*闰年二月29天*/
        final int DAYS_29 = 29;
        /*除了31天的月份,4、6、9、11其他均为30天*/
        final int DAYS_30 = 30;
        /*1、3、5、7、8、10、12月份31天*/
        final int DAYS_31 = 31;
 
        int totalDays = 0;
 
        switch (month) {
            // 12 月份加的是11月份的天数，依次类推
            case 12:
                totalDays += DAYS_30;
            case 11:
                totalDays += DAYS_31;
            case 10:
                totalDays += DAYS_30;
            case 9:
                totalDays += DAYS_31;
            case 8:
                totalDays += DAYS_31;
            case 7:
                totalDays += DAYS_30;
            case 6:
                totalDays += DAYS_31;
            case 5:
                totalDays += DAYS_30;
            case 4:
                totalDays += DAYS_31;
            case 3:
                // 判断是否是闰年
                if (((year / 4 == 0) && (year / 100 != 0)) || (year / 400 == 0)) {
                    totalDays += DAYS_29;
                } else {
                    totalDays += DAYS_28;
                }
            case 2:
                totalDays += DAYS_31;
            case 1: // 如果是1月份就加上输入的天数
                totalDays += day;
        }
        return totalDays;
    }
 
 
 
}
